﻿(function() {
    angular.module('app').controller('manageCtrl', ['$scope', '$timeout', 'catalogSrvc','common',
        function ($scope, $timeout, catalogSrvc, common) {
            activate();
            function activate() {
                $timeout(function () {
                    var promises = [getCars()];
                    common.activateController(promises).then(function() {
                    });
                });
            };
            
            $scope.cars = [];

            var getCars = function () {
                return catalogSrvc.getCars().then(function (data) {
                    return $scope.cars = data;
                });
            };

            $scope.deleteCarById = function (index,id) {
                common.confirm('Do you want to delete this item?',
                    function (result) {
                        if (result) {
                            catalogSrvc.deleteCarById(id).then(function () {
                                $scope.cars.splice(index, 1);
                            });
                        }
                    });
            };

        }]
    );


    angular.module('app').controller('manageAddProdCtrl', ['$scope', '$timeout', 'catalogSrvc','common',
        function ($scope, $timeout, catalogSrvc,common) {
            activate();
            function activate() {
                $timeout(function () {
                    var promises = [getBrandList(), getCarTypeList()];
                    common.activateController(promises).then(function () {
                        init();
                    });
                });
            };

            var getBrandList = function () {
                return catalogSrvc.getCarBrandNames().then(function (data) {
                    return $scope.carBrand = data;
                });
            };

            var getCarTypeList = function () {
                return catalogSrvc.getCartypes().then(function (data) {
                    return $scope.carTypes = data;
                });
            };

            $scope.carBrand = [];

            $scope.carTypes = [];


            $scope.car = {
                Id: 0,
                Name: "",
                Specification: {
                    BrandName: null,
                    CarType: null,
                    VehicleCondition: "",
                    Color: "",
                    Model: ""
                },
                Price: null,
                DatePosted: "",
                LastDateModified: "",
                IsFeatured: false,
                Status: "A",
                Image: [],
                Video: []
            };

            function init() {
                $scope.car = {
                    Id: 0,
                    Name: "",
                    Specification: {
                        BrandName: null,
                        CarType: null,
                        VehicleCondition: "",
                        Color: "",
                        Model: ""
                    },
                    Price: null,
                    DatePosted: common.moment().format(),
                    LastDateModified: common.moment().format(),
                    IsFeatured: false,
                    Status: "A",
                    Image: [],
                    Video: []
                };
            }

            $scope.addCar = function () {
                catalogSrvc.addCars($scope.car).then(function() {
                    $scope.addCarForm.$setPristine();
                    init();
                });                
            };
            $scope.disableSaveBtn = function () {
                if ($scope.addCarForm.$dirty === true) {
                    return $scope.addCarForm.$invalid === true;
                }
                return true;
            };
            
            $scope.navigateBack = function () {
                var navigateUrl = '/catalogs/manage';

                if ($scope.addCarForm.$dirty) {
                    common.confirm('Do you want to navigate away before saving changes?',
                        function(result) {
                            if (result) {
                                $scope.addCarForm.$setPristine();
                                common.redirectToUrl($scope, navigateUrl);
                                
                            } else {
                                $scope.addCarForm.$setDirty();
                            }
                        });
                } else {
                    init();
                    common.redirectToUrl(undefined, navigateUrl);
                }
            };
        }]
    );

    angular.module('app').controller('manageProdDetailsCtrl', ['$scope', '$timeout','$route','catalogSrvc', 'common',
        function ($scope, $timeout, $route, catalogSrvc, common) {
            activate();
            function activate() {
                $timeout(function() {
                    var promises = [getBrandList(), getCarTypeList(), getCarById(params.id)];
                    common.activateController(promises).then(function () {
                        if (params.mode === 'edit') {
                            $scope.editMode = true;
                        }
                    });
                });                
            }
            var params = $route.current.params;

            var getCarById = function (id) {
                return catalogSrvc.getCarById(id).then(function (data) {
                    angular.copy(data, carNonedited);
                    return $scope.car = data;
                }, function () {
                    common.redirectToUrl(undefined, '/notfound');
                });
            };
            
            var getBrandList = function () {
                return catalogSrvc.getCarBrandNames().then(function (data) {
                    return $scope.carBrand = data;
                });
            };

            var getCarTypeList = function () {
                return catalogSrvc.getCartypes().then(function (data) {
                    return $scope.carTypes = data;
                });
            };

            $scope.carBrand = [];

            $scope.carTypes = [];

            var carNonedited = {};
           
            $scope.car = {
                Id: null,
                Name: null,
                Specification: {
                    BrandName: null,
                    CarType: null,
                    VehicleCondition: null,
                    Color: null,
                    Model: null
                },
                Price: null,
                DatePosted: "",
                LastDateModified: "",
                IsFeatured: false,
                Status: "A",
                Image: [],
                Video: []
            };

            $scope.editMode = false;
            $scope.switchToEditMode = function () {
                $scope.editMode = true;
            };
            $scope.cancelEdit = function () {
                angular.copy(carNonedited, $scope.car);
                $scope.editMode = false;
                $scope.editCarForm.$setPristine();
            };
            $scope.editCar = function () {
                catalogSrvc.editCar($scope.car).then(function () {
                    $scope.editCarForm.$setPristine();
                    $scope.editMode = false;
                });
                
            };
            $scope.disableEditBtn = function () {
                if ($scope.editCarForm.$dirty === true) {
                    return $scope.editCarForm.$invalid === true;
                }
                return true;
            };
            
            $scope.navigateBack = function () {
                var navigateUrl = '/catalogs/manage';
                if ($scope.editCarForm.$dirty) {
                    common.confirm('Do you want to navigate away before saving changes?',
                        function (result) {
                            if (result) {
                                common.redirectToUrl($scope, navigateUrl);
                                $scope.editCarForm.$setPristine();
                            } else {
                                $scope.editCarForm.$setDirty();
                            }
                        });
                } else {
                    common.redirectToUrl(undefined, navigateUrl);
                }
            };
        }]
    );

    angular.module('app').controller('CategoriesCtrl', ['$scope', function ($scope) {
        $scope.HelloWorld = 'Hello World2';
    }]);

    angular.module('app').controller('uploadCtrl',
        function ($scope, uploadService) {
            $scope.uploadLayer = function (e, data, process) {
            console.log(data);
            return $scope.uploadReturn = uploadService.process(e, data, process);
        };
        $scope.customData = function() {
            var data;
            return data = {
                firstname: $scope.firstname,
                lastname: $scope.lastname
            };
        };
        return $scope.uploadReturn = uploadService.initialize();
    });
})();